module(..., package.seeall) -- 使得文件中的函数在何处都可调用
require "pins"              -- 用到了pin库，该库为luatask专用库，需要进行引用
require "sys"
require "audio"
require "math"
require "nvm"
require "modbusrtu"
nvm.init("config.lua", true)
local countDownSDrill, quake_intensity, magnitudeDevice, drillBreak, doDrillTask =
    0, 0, 0.0, false, false
local setGpio16Fnc = pins.setup(pio.P0_16, 1)     -- 电磁阀吸合 0-正常 1-吸合
local setGpio17Fnc = pins.setup(pio.P0_17, 1)     -- 报警灯 0-灯灭 1-灯亮
TTS = 1
-- 获取配置参数
local deviceMagnitude, configIntensity = nvm.get("DeviceMagnitude"),
    nvm.get("ConfigIntensity")
local function intensity2SeismicCategory(intensity2Th)
    -- 利用公式求出设备所在地的地震等级(华北地区，b半径)
    magnitudeDevice = (intensity2Th - 2.008 + 2.958 * math.log10(7)) / 1.434
    return magnitudeDevice
end
-- 电磁阀动作
local function electromagneticValveOperation(countDowns)
    doDrillTask = true
    setGpio16Fnc(0)
    setGpio17Fnc(0)
    while countDownSDrill >= 0 do
        if drillBreak then
            drillBreak = false
            break
        end
        log.info("countDownSDrill:", countDownSDrill)
        sys.wait(1000)
        countDownSDrill = countDownSDrill - 1
        if drillBreak then
            drillBreak = false
            break
        end
    end
    setGpio16Fnc(1)
    setGpio17Fnc(1)
    doDrillTask = false
end
-- 地震预警系统
-- 两个功能：1.预警系统演练（tts语音播报3遍同时报警音触发）
-- 入参：mqttData接收的预警信息
function EarthquakeWarningDrillSystem(tjsondata)
    if doDrillTask then drillBreak = true end
    while doDrillTask do sys.wait(100) end
    deviceMagnitude, configIntensity = nvm.get("DeviceMagnitude"), nvm.get("ConfigIntensity")
    local msgCode, tClock, sysTime, eqlogInfo, errInfo = nil, os.date("*t"), "", "", ""
    sysTime = string.format("%04d-%02d-%02d %02d:%02d:%02d", tClock.year,
        tClock.month, tClock.day, tClock.hour, tClock.min,
        tClock.sec)

    --[[ if not tjsondata["countDowns"] then
        countDownSDrill = 0
    else
        countDownSDrill = tonumber(tjsondata["countDowns"])
    end
    if not tjsondata["intensity"] then
        quake_intensity = 0
    else
        quake_intensity = tonumber(tjsondata["intensity"])
    end ]]
    if not tjsondata["1"] then
        countDownSDrill = 0
    else
        countDownSDrill = tonumber(tjsondata["1"])
    end
    if not tjsondata["2"] then
        quake_intensity = 0
    else
        quake_intensity = tonumber(tjsondata["2"])
    end
    if tjsondata["16"] then msgCode = tjsondata["16"] end
    --[[ if tjsondata["msgCode"] then msgCode = tjsondata["msgCode"] end ]]
    log.info("tacountDownSDrillg", countDownSDrill, "quake_intensity",
        quake_intensity, "msgCode", msgCode)
    if msgCode == "0199" or quake_intensity == 0 then
        eqlogInfo = eqlogInfo .. "[" .. sysTime .. "]" ..
            "预警演习取消！级\n"
        if doDrillTask then drillBreak = true end
    else
        eqlogInfo = eqlogInfo .. "[" .. sysTime .. "]" ..
            "[预警演习:]到达秒数:" .. countDownSDrill ..
            "s,地震烈度:" .. quake_intensity .. "级\n"
        -- 压力切断装置
        if quake_intensity >= configIntensity or
            intensity2SeismicCategory(quake_intensity) >= deviceMagnitude then
            eqlogInfo = eqlogInfo .. "[" .. sysTime .. "]" ..
                "本地计算-达到本地预警阀值！\n"
            if countDownSDrill >= 0 then
                sys.taskInit(electromagneticValveOperation, countDownSDrill)
            end
        else
            eqlogInfo = eqlogInfo .. "[" .. sysTime .. "]" ..
                "[不满足预警临界值：]" .. "烈度：" ..
                quake_intensity .. ",烈度预设值：" ..
                configIntensity .. ",震中烈度:" ..
                quake_intensity .. "\n"
        end
    end
    if eqlogInfo ~= "" and eqlogInfo ~= false then
        log.info("生成eq日志")
        -- logGenerator.generateLogTask(eqlogInfo, "log")
    end
    if errInfo ~= "" and errInfo ~= false then
        log.info("生成错误日志", errInfo)
        -- logGenerator.generateLogTask(errInfo, "log")
    end
end
